using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;

public partial class Admin_AdminSecureLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.Validate();
        }

        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) ==
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("AdminHome.aspx");
            }
        }
    }
    protected void btnSignIn_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.CheckEmail(txtAdminEmailAddress.Text.ToLower(), Convert.ToInt32(CE.Global.CEUserType.SiteAdmin));

            if (_account != null)
            {
                if (txtAdminAuthCode.Text == _account.AuthCode && _account.UserTypeId == Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
                {
                    DateTime dtNow = new DateTime();
                    dtNow = DateTime.Now;

                    Session[CE.Global.General.UserId] = _account.CEID;
                    Session[CE.Global.General.UserTypeId] = _account.UserTypeId;
                    Session[CE.Global.General.UserEmail] = _account.Email;
                    Session[CE.Global.General.StartTime] = dtNow.ToString();
                    

                    ProfileDB _profiledb = new ProfileDB();
                    Profile _profile = new Profile();
                    Contact _contact = new Contact();
                    ContactDB _contactdb = new ContactDB();



                    _contact = _contactdb.GetContact(_account.CEID);

                    _profile = _profiledb.GetProfile(_account.CEID);

                    if (_contact != null)
                    {
                        Session[CE.Global.General.UserPhoneNo] = _contact.HomePhone;
                    }
                    else
                    {
                        Session[CE.Global.General.UserPhoneNo] = "No Phone No";
                    }

                    if (_profile != null)
                    {

                        Session[CE.Global.General.UserName] = _profile.Salutation + " " + _profile.FirstName;
                    }
                    else
                    {

                        Session[CE.Global.General.UserName] = "No Name";
                    }

                    LoginHistory _loghis = new LoginHistory();
                    _loghis.CEID = _account.CEID;
                    _loghis.UserTypeId = _account.UserTypeId;
                    _loghis.StartTime = dtNow;
                    _loghis.EndTime = dtNow;
                    _loghis.IsExpired = false;
                    _loghis.Status = "LOGIN";
                    LoginHistoryDB _loghisdb = new LoginHistoryDB();

                    Session[CE.Global.General.LoginHistoryId] = _loghisdb.Insert(_loghis).ToString();
                    Response.Redirect("AdminHome.aspx");

                    //if (Session[CE.Global.General.RequestUrl] != null)
                    //{
                    //    string redirectUrl = Session[CE.Global.General.RequestUrl].ToString();
                    //    Session[CE.Global.General.RequestUrl] = null;
                    //    Response.Redirect(redirectUrl);
                    //}
                    //else
                    //{
                    //    Response.Redirect("AdminHome.aspx");
                    //}
                }
                else
                {
                    lblMessage.Text = "Password wrong.";
                }
            }
            else
            {
                lblMessage.Text = "Email address wrong.";
            }
        }
    }
}
